<?php

ignore_user_abort(true); // 后台运行，不受前端断开连接影响
set_time_limit(0);
ini_set('max_execution_time', '0');
chdir(dirname(__FILE__));
// error_reporting(E_ERROR);

require_once ("../../config.php");//引入配置ss
require_once ("../../log.php");
require_once ("../api_tool.php");
require_once ("core.php");

// simQuery("TRUNCATE TABLE item_ordinary");

$ic_list = AllQuery("SELECT * FROM item_category WHERE channel_id = 2 AND status = '1';");

$repeat_shop_id = 0;
foreach ($ic_list as $ic_value) {
	// 页码 (京东单页最大访问量为30,超过30需要重新更换页面)
	for ($page = 1; $page <= 10; ++$page) {
		$resp = getJdUnionOpenGoodsQuery($ic_value['category_id'], '', $page);

	 	if (empty($resp['data'])) {
			break;
		}

		foreach ($resp['data'] as $map_data) {
			if ($repeat_shop_id == $map_data['shopInfo']['shopId']) {
				break;
			}

			var_dump($ic_list);
			$repeat_shop_id = $map_data['shopInfo']['shopId'];
			$detail_images = '';
			foreach ($map_data['imageInfo']['imageList'] as $key => $value) {
				$detail_images .= $value['url'];
				end($map_data['imageInfo']['imageList']);
				if ($key !== key($map_data['imageInfo']['imageList'])) {
					$detail_images .= ",";
				}
			}

			$coupon_url = '';
			$coupon_amount = '';
			$coupon_start_fee = '';

			foreach ($map_data['couponInfo']['couponList'] as $key => $value) {
				$coupon_url .= $value['link'];
				$coupon_amount .= $value['discount'];
				$coupon_start_fee .= $value['quota'];
				end($map_data['couponInfo']['couponList']);
				if ($key !== key($map_data['couponInfo']['couponList'])) {
					$coupon_url .= ",";
					$coupon_amount .= ",";
					$coupon_start_fee .= ",";
				}
			}

			$parent_category_ids = ',0,';
			if (!empty($ic_value['parent_category_ids'])) {
				$parent_category_ids .= "{$ic_value['parent_category_ids']},";
			}

			$good_list = array (
				'item_id'				=> $map_data['skuId'],								// 商品ID
				'name'					=> $map_data['skuName'],							// 商品名称
				'title'					=> $map_data['skuName'],							// 商品标题
				'image' 				=> $map_data['imageInfo']['imageList'][0]['url'],	// 商品图片
				'url'					=> "https://{$map_data['materialUrl']}",			// 商品图片
				'price' 				=> $map_data['priceInfo']['lowestPrice'],			// 商品价格
				'detail_images'			=> $detail_images,									// 展示图册
				'commission_rate' 		=> $map_data['commissionInfo']['commissionShare'],	// 佣金比率
				'coupon_url' 			=> $coupon_url,										// 领券链接
				'coupon_amount' 		=> $coupon_amount,									// 优惠券面值
				'coupon_start_fee' 		=> $coupon_start_fee,								// 优惠券起用金额
				'channel_id' 			=> 2,												// 渠道代码:1=淘宝, 2=京东, 3=拼多多
				'category_id' 			=> $map_data['categoryInfo']['cid1'],				// 商品类别ID
				'category_name' 		=> $map_data['categoryInfo']['cid1Name'],			// 商品类别
				'sub_category_id' 		=> $map_data['categoryInfo']['cid3'],				// 商品子类别ID
				'sub_category_name' 	=> $map_data['categoryInfo']['cid3Name'],			// 商品子类别
				'nick' 					=> $map_data['shopInfo']['shopName'],				// 店铺昵称
				'parent_category_ids'	=> $parent_category_ids,							// 主目录id
			);

			$t_key = "";
			$t_value = "";
			foreach ($good_list as $key => $value) {
				$t_key .= $key;
				$t_value .= "'{$value}'";
				end($good_list);
				if ($key !== key($good_list)) {
					$t_key .= ', ';
					$t_value .= ', ';
				}
			}
			simQuery("INSERT INTO item_ordinary ({$t_key}) VALUES ({$t_value})");
		}
	}
}



?>